package com.smart.store.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@Component
@Slf4j
public class TokenFilter implements Filter {

    @Value("${login.x-token:token}")
    private String xToken;


    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest)servletRequest;
        String token = httpServletRequest.getHeader("x-token");
        if (xToken.equals(token)) {
            filterChain.doFilter(servletRequest, servletResponse);
        } else {
            log.error("no auth with token {}, " + token);
        }
    }
}
